package com.android.bolos;

import java.text.DecimalFormat;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;

public class Estadisticas extends Activity {
	private SQLiteDatabase baseDatos;
	private TextView totaldebolos, numeroconcursos, mediadebolos;
  
	private static final String TAG = "bdbolos";   
	static final String nombreBD = "concursos";   
	private static final String tablageneral = "general";  

	
	//guardamos en un String toda la creación de la tabla        
	  private static final String creartablageneral = "create table if not exists "  
		  + " general (_id integer primary key autoincrement, "  
	  	  + " fecha text not null, " 
	  	  + " tiro1 text not null, tiro2 text not null, tiro3 text not null, tiro4 text not null, tiro5 text not null, tiro6 text not null, tiro7 text not null, tiro8 text not null, "
	  	  + " birle1 text not null, birle2 text not null, birle3 text not null, birle4 text not null, birle5 text not null, birle6 text not null, birle7 text not null, birle8 text not null, "
		  + " totalgeneral text not null, lugar text, tipo text, hora text );";
	  
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.estadisticas);
	    //Asignamos los objetos del layout
	    this.numeroconcursos = (TextView) findViewById(R.id.numeroconcursos); 
	    this.totaldebolos = (TextView) findViewById(R.id.totaldebolos);
	    this.mediadebolos = (TextView) findViewById(R.id.mediadebolos);
	    
	    // TODO Auto-generated method stub
	    // abrimos la base de datos
	    try 
	      {   
	        baseDatos = openOrCreateDatabase(nombreBD, MODE_WORLD_WRITEABLE, null);   
	        baseDatos.execSQL(creartablageneral);   
	      }    
	      catch (Exception e)
	      {   
	        Log.i(TAG, "Error al abrir o crear la base de datos" + e);   
	      }
	    // consulta que selecciona todos los concursos
	    Cursor c = baseDatos.rawQuery("SELECT * FROM general", null); 
	    
	    // total de bolos derribados
	    c.moveToFirst();
	    double imax = c.getCount();
	    double sumatotal = 0;
	    for (int i = 1; i <= imax; i++)
	  {
	   
	    sumatotal=sumatotal+ Integer.parseInt(c.getString(18));	
	    c.moveToNext();
	    }
double media = sumatotal / imax; 
DecimalFormat df = new DecimalFormat("0.00"); 
DecimalFormat df2 = new DecimalFormat("0"); 
	numeroconcursos.setText(df2.format(imax));
	totaldebolos.setText(df2.format(sumatotal));
	mediadebolos.setText(df.format(media));
	    
	}
	
		
}
